﻿package XFComponent
{
	import flash.display.DisplayObject;
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	
	public class XFProgressBar extends Sprite
	{
		protected var _bitData : BitmapData;
		protected var _bitmap : Bitmap;
		protected var _asset : DisplayObject;
		private var _loadingIcon : DisplayObject;
		
		protected var _value : Number		=	0;
		protected var _max : Number			=	1;
		protected var _min : Number			=	0;
		protected var _assetWidth : Number	=	1;
		protected var _assetHeight : Number	=	1;
		
		public function XFProgressBar( width : Number, height : Number )
		{
			super();
			
			_assetWidth		=	width;
			_assetHeight	=	height;
			
			_bitmap			=	new Bitmap();
			addChild( _bitmap );
			
			_loadingIcon			=	new XFLoadingIcon;
			
			updateLoadingIcon();
			
			draw();
		}
		
		public function get assetHeight():Number
		{
			return _assetHeight;
		}

		public function set assetHeight(value:Number):void
		{
			_assetHeight = value;
		}

		public function get assetWidth():Number
		{
			return _assetWidth;
		}

		public function set assetWidth(value:Number):void
		{
			_assetWidth = value;
		}

		public function set assetSkin( val : DisplayObject ) : void
		{
			if( _asset == val )
				return;
			
			_asset		=	val;		
			
			updateLoadingIcon();
			draw();
		}
		
		public function set changeAssetSkin( val : DisplayObject ) : void
		{
			if( _asset == val )
				return;
			
			_asset		=	val;		
			draw();
		}
		
		public function get assetSkin() : DisplayObject
		{
			return _asset;	
		}
		
		private function draw():void
		{			
			if( _asset == null )
				return;
				
			var w : Number		=	((_value - _min) / (_max - _min)) * _assetWidth;
			if( int(w) <= 0)
			{
				_bitmap.bitmapData	=	null;
				return;
			}
				
			_bitData	= new BitmapData( w, _assetHeight, true, 0x00000000 );
			_bitData.draw( _asset );
			
			_bitmap.bitmapData	=	_bitData;
		}
		
		/**
		 * Gets / sets the maximum value of the ProgressBar.
		 */
		public function set maximum(m:Number):void
		{
			if( m < _min )
				m	=	_min + 1;
				
			_max = m;
			_value = Math.min( _value, _max );
			draw();
		}
		public function get maximum():Number
		{
			return _max;
		}
		
		/**
		 * Gets / sets the maximum value of the ProgressBar.
		 */
		public function set minimum(m:Number):void
		{
			if( m > _max )
				m	=	_max - 1;
				
			_min = m;
			_value = Math.max( _min, _value );
			draw();
		}
		public function get minimum():Number
		{
			return _min;
		}
		
		/**
		 * Gets / sets the current value of the ProgressBar.
		 */
		public function set value(v:Number):void
		{
			if( v < 0 )
				v = 0.0;
			else
			{
				_value	=	Math.max( v, _min );
				_value 	= 	Math.min( v, _max );		
			}
			
			draw();
		}
		
		public function get value():Number
		{
			return _value;
		}
		
		private function updateLoadingIcon() : void
		{
			// Remove loading icon
			if( _asset != null )
			{
				removeChild( _loadingIcon );
				_loadingIcon = null;
			}
			else
			{
				addChild( _loadingIcon );
			}
			
		}
	}
}